<template>
<page-container title="更换头像">
  <el-upload
  class="avatar-uploader"
  :auto-upload="false"
  :show-file-list="false"
  :on-change="onSelectFile">
  <img v-if="imageUrl" :src="imageUrl" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  </el-upload>

    <el-button
      @click="handleSelectFileClick"
      style="margin-top: 20px;"
      type="primary"
      :icon="Plus"
      size="large"
      >选择图片</el-button
    >
    <el-button
      @click="onUpdateAvatar"
      style="margin-top: 20px;"
      type="success"
      :icon="Upload"
      size="large"
      >上传头像</el-button
    >
</page-container>

</template>

<script>
import PageContainer from '@/components/PageContainer.vue'

export default {
  components: {
    PageContainer
  },
  data () {
    return {
      imageUrl: null // 存储头像图片的 URL
    }
  },
  methods: {
    handleSelectFileClick () {
      this.$refs.uploadRef.$el.querySelector('input').click()
    },
    onSelectFile (uploadFile) {
      const reader = new FileReader()
      reader.onload = (e) => {
        this.imageUrl = e.target.result // 预览图片
      }
      reader.readAsDataURL(uploadFile.raw)
    },
    onUpdateAvatar () {
      // 这里可添加实际上传逻辑，如调用接口
      console.log('执行上传头像操作，此处可调用接口上传图片')
    }
  }
}
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  i.el-icon-plus.avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
